using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ChatEntry
{
    public string pseudo;
    public string msg;
	public Color color;

    public ChatEntry(string name, string msg, string stringColor)
    {
        this.pseudo = name;
        this.msg = msg;
		// utiliser un case par la suite
		if ( stringColor == "white" )
		{
			color = Color.white;
		}
		else if ( stringColor == "yellow" )
		{
			color = Color.yellow;
		}
		else if ( stringColor == "red")
		{
			color = Color.red;
		}
		else
		{
			color = Color.white;
		}
    }
}

public class Chat : MonoBehaviour
{
    public int networkGroup = 1;
    public string inputField = "";
    public Rect windowChat = new Rect(10, 10, 100, 200);
    public int historySize = 50;
    public bool isDraggable = false;
    public string pseudo = "";
    public int chatID = 7;

    private List<ChatEntry> chatEntries;
    private List<ChatEntry> chatEntriesToDisplay;
    private Vector2 chatScrollPosition;
    private int lastCountCE = 0;

    public void Awake()
    {
        networkView.group = networkGroup; //set the group for communication
        chatEntries = new List<ChatEntry>();
        chatEntriesToDisplay = new List<ChatEntry>();
        lastCountCE = chatEntriesToDisplay.Count;
    }

    public void Update()
    {
        //update chat list. Prevent an error of GUILayout
        chatEntriesToDisplay = new List<ChatEntry>(chatEntries);
    }

    public void DrawChat(Rect rect)
    {
		windowChat = rect;
        windowChat = GUILayout.Window(chatID, windowChat, ChatWindow, "Chat");
    }

    public void ChatWindow(int id)
    {
        //auto scroll to the last chat entry
        if (lastCountCE < chatEntriesToDisplay.Count)
        {
            chatScrollPosition.y = 100000;
            lastCountCE = chatEntriesToDisplay.Count;
        }

        //chatbox
        chatScrollPosition = GUILayout.BeginScrollView(chatScrollPosition);
        foreach (ChatEntry entry in chatEntriesToDisplay)
        {
            GUILayout.BeginHorizontal();
			GUI.color = entry.color;
            GUILayout.Label(entry.pseudo + " : " + entry.msg);
            GUILayout.EndHorizontal();
            GUILayout.Space(2);
        }
		GUI.color = Color.white;
        // End the scrollview we began above.
        GUILayout.EndScrollView();

        //input field

        GUILayout.BeginHorizontal();
        GUI.SetNextControlName("input field");
        inputField = GUILayout.TextField(inputField, GUILayout.MinWidth(100));
        GUI.FocusControl("input field");

        Event e = Event.current;
        if (e.keyCode == KeyCode.Return && inputField.Length > 0)
        {
            SubmitMsg(inputField);
        }
        if (GUILayout.Button("Send", GUILayout.MinWidth(70), GUILayout.ExpandWidth(false)) && inputField.Length > 0)
        {
            SubmitMsg(inputField);
        }
        GUILayout.EndHorizontal();

        if (isDraggable)
        {
            GUI.DragWindow();
        }
    }

    public void SubmitMsg(string msg)
    {
        msg = msg.Replace("\n", "");
        networkView.RPC("NewChatEntry", RPCMode.All, pseudo, msg,"white");
        inputField = ""; //Clear line
        GUI.FocusControl("input field");
    }

    [RPC]
    public void NewChatEntry(string name, string text, string color="white")
    {
        chatEntries.Add(new ChatEntry(name, text, color));
        if (chatEntries.Count > historySize)
        {
            chatEntries.RemoveAt(0);
        }
    }

    public void SetWindowChat(float x, float y, float width, float height)
    {
        windowChat = new Rect(x, y, width, height);
    }

    /// <summary>
    /// Clear the entries of the chat
    /// </summary>
    public void ClearChat()
    {
        chatEntries.Clear();
        chatEntriesToDisplay.Clear();
        lastCountCE = 0;
    }

    public void AddAnnonce(string annonce, string name="Server", string color="white")
    {
        networkView.RPC("NewChatEntry", RPCMode.All, name, annonce, color);
    }

    public void OnPlayerConnected(NetworkPlayer player)
    {
        AddAnnonce("new player");
    }

    public void OnDisconnectedFromServer()
    {
        ClearChat();
    }
}
